public class MySingleList implements IList{
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int data){
            this.val=data;
        }
    }
    public ListNode head;//链表的头

    // 1、无头单向非循环链表实现
    @Override
    //头插法
    public void addFirst(int data) {
        ListNode listNode=new ListNode(data);//该节点是新的头节点
        listNode.next=head;
        head=listNode;
    }

    @Override
    public void addLast(int data) {
        ListNode listNode=new ListNode(data);
        if(head==null){
            head=listNode;
            return;
        }
        ListNode cur=head;
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=listNode;
    }

    @Override
    public void addIndex(int index, int data) {
        int len=size();
        if (index<0||index>len){
            new IndexOutOfBoundary("index输入有误");
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==len){
            addLast(data);
            return;
        }
        int curLen=0;
        ListNode cur=head;
        ListNode listNode=new ListNode(data);
        while(curLen<index-1){
            curLen++;
            cur=cur.next;
        }
        listNode.next=cur.next;
        cur.next=listNode;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur=new ListNode(key);
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head==null){//链表为空
            return;
        }
        if(head.val==key){//删除的位置在头节点
            head=head.next;
            return;
        }
        ListNode pre=findPreNodeOfKey(key);
        if(pre!=null){
            ListNode del=pre.next;//找到需要删除的节点
            pre.next=del.next;
        }

    }
    private  ListNode findPreNodeOfKey(int key){
        ListNode cur=head;
        while(cur.next!=null){//最后一个节点也已经判断过了
            if (cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }



    @Override
    public void removeAllKey(int key) {
        if(head==null){
            return;
        }
        ListNode prev=head;
        ListNode cur=head.next;
        while(cur!=null){
             if(cur.val==key){
                 prev.next=cur.next;
                 cur= cur.next;
             }else {
                 prev=prev.next;
                 cur= cur.next;
             }
        }
        //把前面的除头节点之外的都删完之后删除头节点
        if (head.val==key){
            head=head.next;
        }
    }

    @Override
    public int size() {
        int len=0;
        ListNode cur=head;
        while (cur!=null){
            len++;
            cur=cur.next;
        }
        return len;
    }

    @Override
    public void clear() {
        ListNode cur=head;
        while (cur!=null){
            ListNode curN=cur.next;
            cur.next=null;//基本数据类型的val不需要回收
            cur=curN;
        }
    }

    @Override
    public void display() {
        ListNode cur=head;//不能改变头的引用
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }

        public boolean chkPalindrome() {
            if(head==null){
                return true;
            }
            // write code here
            ListNode slow=head;
            ListNode fast=head;
//找到中间节点
            while(fast!=null&&fast.next!=null){
                fast=fast.next.next;
                slow=slow.next;
            }
//翻转
            ListNode cur=slow.next;
            while(cur!=null){
                ListNode curN=cur.next;
                cur.next=slow;
                slow=cur;
                cur=curN;
            }
//从左右分别判断
            while(head!=slow){
                if(head.val!=slow.val){
                    return false;
                }
                if(head.next==slow){
                    return true;
                }
                slow=slow.next;
                head=head.next;
            }
            return true;
        }



}


